package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.TokenBo;
import com.zhuss.bean.bo.core.login.wechat.InitBo;
import com.zhuss.bean.bo.core.user.CreateUserBo;
import com.zhuss.bean.unit.core.user.UserBean;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.bean.vo.core.login.LoginVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.ListVo;
import com.zhuss.common.Processor;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.RoleType;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 初始化 processor
 * @Author: zhushanshuo
 * @Date: 2020/03/30 17:55
 */
@Component("initProcessor")
public class InitProcessor extends AbstractProcessor<LoginVo, InitBo> implements Processor<LoginVo, InitBo> {

	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
	
	@Autowired
	@Qualifier("userRoleRead")
	private UserRoleRead userRoleRead;
	
	@Autowired
	@Qualifier("roleRead")
	private RoleRead roleRead;
	
	@Autowired
	@Qualifier("userCreate")
	private Processor<Object, CreateUserBo> userCreate;
	
	@Autowired
	@Qualifier("loginRedis")
	private Processor<Void, TokenBo> loginRedis;
	
    /**
     * 校验
     */
    @Override
    public void check(InitBo bo) throws OceanException {
    	String openId = bo.getOpenId();
    	
    	if ((openId == null || "".equals(openId.trim()))) {
    		throw new OceanException(ExceptionCode.OPEN_ID为空);
    	}
    	
    	List<User> userList = userRead.listByOpenId(openId);
    	if (userList == null || userList.isEmpty()) {
    		List<Long> roleIdList = roleRead.listByType(RoleType.访客.key());
    		CreateUserBo createUserBo = new CreateUserBo();
    		createUserBo.setRoleIdList(roleIdList);
    		createUserBo.setOpenId(openId);
    		createUserBo.setIsBind(IsEnum.否.key());
    		userCreate.process(createUserBo);
    		TokenBo paramToken = new TokenBo();
        	BeanUtils.copyProperties(bo, paramToken);
        	paramToken.setToken(openId);
        	paramToken.setUserId(createUserBo.getTargetId());
        	loginRedis.process(paramToken);
    	}
    	
    	User userDb = null;
    	userList = userRead.listByOpenId(openId);
    	if (userList != null && userList.size() <= 2) {
    		User user1 = userList.get(0);
    		userDb = user1;
    		
    		User user2 = null;
    		if (userList.size() > 1) {
    			user2 = userList.get(1);
    			if (user2.getUsername() != null && !"".equals(user2.getUsername())) {
    				userDb = user2;
    			}
    		}
    	}
    	
    	
    	
    	
    	UserBean user = new UserBean();
    	bo.setTargetId(userDb.getUserId());
    	BeanUtils.copyProperties(userDb, user);
    	bo.setUser(user);
    	
    	Long companyId = user.getCompanyId();
    	if (companyId != null && companyId != 0l) {
    		DetailCompanyVo company = (DetailCompanyVo) companyRead.detail(companyId);
    		String companyName = company.getName();
    		user.setCompanyName(companyName);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws OceanException 
     */
	@Override
	@SuppressWarnings("unchecked")
    public LoginVo process(InitBo bo) throws OceanException {
    	String openId = bo.getOpenId();
    	String token = openId == null || "".equals(openId.trim()) ? UUID.randomUUID().toString() : openId;
    	UserBean user = bo.getUser();
    	Long userId = user.getUserId();
    	LoginVo result = new LoginVo();
    	result.setUser(user);
    	result.setToken(token);
    	List<Long> roleIdList = (List<Long>) userRoleRead.listRoleId(userId);
    	if (roleIdList != null && !roleIdList.isEmpty()) {
    		List<ListVo> roleList = (List<ListVo>) roleRead.getByList(roleIdList);
    		result.setRoleList(roleList);
    	}
    	
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public LoginVo log(InitBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户请求init.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName("用户ID:" + param.getTargetId());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getTargetId());
    	record.setCreator("用户ID:" + param.getTargetId());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
